﻿(function() {
    $(function() {
        var _personService = abp.services.app.person;

        var _$modal = $("#PersonCreateModal");
        var _$form = _$modal.find("form");

        //添加联系人功能
        _$form.find('button[type="submit"]').click(function(e) {
            e.preventDefault();//阻止button 默认提交
            if (!_$form.valid()) {
                return;
            }

            var personEditDto = _$form.serializeFormToObject();//序列化表单为对象

            //防重复提交
            abp.ui.setBusy(_$modal);
            //约定大于配置
            _personService.createOrUpdatePerson({ personEditDto }).done(function() {
                _$modal.modal("hide");
                refreshPersonList();
            }).always(function () {
                //去除防重复提交
                abp.ui.clearBusy(_$modal);
            });
        });
        $("#RefreshButton").click(function() {
            refreshPersonList();
        });
        $('.delete-person').click(function() {
            var personid = $(this).attr("data-person-id");
            var personName = $(this).attr("data-person-name");
            deletePerson(personid,personName)
        });
        $(".edit-person").click(function(e) {
            e.preventDefault();
            var personId = $(this).attr("data-person-id");
            _personService.getPersonForEdit({ id: personId }).done(function(data) {
                $("input[name=Id]").val(data.person.id);
                $("input[name=Name]").val(data.person.name).parent().addClass("focused");
                $("input[name=Email]").val(data.person.email).parent().addClass("focused");
                $("input[name=Address]").val(data.person.address).parent().addClass("focused");
            });
        });
        $("#PersonCreateModal").on("hide.bs.modal",
            function() {
                _$form[0].reset();
            });
        function deletePerson(id,name) {
            abp.message.confirm("是否确认删除姓名为：" + name + "的联系人",
                function(isConfirmed) {
                    if (isConfirmed) {
                        _personService.deletePerson({ id }).done(function() {
                            refreshPersonList();
                        });
                    }
                });
        };
        function refreshPersonList() {
            location.reload();
        };
    })
})();